package com.example.jikeyoujikeyou.office_sz.view.activity.register;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.comment.utils.ActivityTask;
import com.example.jikeyoujikeyou.office_sz.comment.utils.ListValidate;
import com.example.jikeyoujikeyou.office_sz.model.Department;

import java.util.ArrayList;
import java.util.List;

public class ChoiceDepartmentAdapter extends BaseAdapter {
    int currentPosition;
    private List<Department> departmentList = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    public List<Department> getDepartmentList() {
        return departmentList;
    }

    public void setDepartmentList(List<Department> departmentList) {
        this.departmentList = departmentList;
    }

    public ChoiceDepartmentAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return ListValidate.isEmpty(departmentList) ? 0 : departmentList.size();
    }

    public void addItem(Department department){
        departmentList.add(department);
        notifyDataSetChanged();
    }

    @Override
    public Department getItem(int position) {
        return departmentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ChoiceDepartmentAdapter.ViewHolder viewHolder=null;
        if(null == view){
            view = inflater.inflate(R.layout.item_department_select, null, false);
            viewHolder= new ChoiceDepartmentAdapter.ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder= (ChoiceDepartmentAdapter.ViewHolder) view.getTag();
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Department department = getItem(position);
                Intent intent = new Intent();
                intent.putExtra("department_name",department.getName());
                ActivityTask.getInstance().getCurrentShowActivity().setResult(200,intent);
                ActivityTask.getInstance().getCurrentShowActivity().finish();
            }
        });
        viewHolder.tv_department_name.setText(getItem(position).getName());
        return view;
    }

    class ViewHolder {

        View converView;
        TextView tv_department_name;

        public ViewHolder(View converView) {
            this.converView = converView;
            tv_department_name = converView.findViewById(R.id.tv_department_name);
        }
    }
}
